package com.openbravo.pos.inventory;

import com.openbravo.basic.BasicException;
import com.openbravo.data.gui.MessageInf;
import com.openbravo.data.user.DirtyManager;
import com.openbravo.data.user.EditorRecord;
import com.openbravo.format.Formats;
import com.openbravo.pos.forms.AppLocal;
import com.openbravo.pos.forms.AppView;
import com.openbravo.pos.forms.DataLogicSales;
import com.openbravo.pos.panels.JProductFinder;
import com.openbravo.pos.ticket.ProductInfoExt;

import java.awt.Component;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.UUID;

import javax.swing.GroupLayout;
import javax.swing.GroupLayout.Alignment;
import javax.swing.GroupLayout.ParallelGroup;
import javax.swing.GroupLayout.SequentialGroup;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.LayoutStyle;
import javax.swing.LayoutStyle.ComponentPlacement;
import javax.swing.text.Document;

public class AuxiliarEditor extends JPanel
  implements EditorRecord
{
  private DataLogicSales m_dlSales;
  private Object id;
  private Object product;
  private Object product2;
  private Object name;
  private Object insertproduct;
  private JLabel jLabel3;
  private JLabel jLabel4;
  private JTextField m_jBarcode;
  private JButton m_jEnter1;
  private JButton m_jEnter2;
  private JTextField m_jProduct;
  private JTextField m_jReference;
  private JButton m_jSearch;

  public AuxiliarEditor(AppView paramAppView, DirtyManager paramDirtyManager)
  {
    this.m_dlSales = ((DataLogicSales)paramAppView.getBean("com.openbravo.pos.forms.DataLogicSales"));
    initComponents();
    this.m_jProduct.getDocument().addDocumentListener(paramDirtyManager);
  }

  public void setInsertProduct(ProductInfoExt paramProductInfoExt)
  {
    if (paramProductInfoExt == null)
      this.insertproduct = null;
    else
      this.insertproduct = paramProductInfoExt.getID();
  }

  public void refresh()
  {
  }

  public void writeValueEOF()
  {
    this.id = null;
    this.product = null;
    this.product2 = null;
    this.name = null;
    this.m_jReference.setText(null);
    this.m_jBarcode.setText(null);
    this.m_jProduct.setText(null);
    this.m_jReference.setEnabled(false);
    this.m_jBarcode.setEnabled(false);
    this.m_jProduct.setEnabled(false);
    this.m_jEnter1.setEnabled(false);
    this.m_jEnter2.setEnabled(false);
    this.m_jSearch.setEnabled(false);
  }

  public void writeValueInsert()
  {
    this.id = UUID.randomUUID().toString();
    this.product = this.insertproduct;
    this.product2 = null;
    this.name = null;
    this.m_jReference.setText(null);
    this.m_jBarcode.setText(null);
    this.m_jProduct.setText(null);
    this.m_jReference.setEnabled(true);
    this.m_jBarcode.setEnabled(true);
    this.m_jProduct.setEnabled(true);
    this.m_jEnter1.setEnabled(true);
    this.m_jEnter2.setEnabled(true);
    this.m_jSearch.setEnabled(true);
  }

  public void writeValueEdit(Object paramObject)
  {
    Object[] arrayOfObject = (Object[])(Object[])paramObject;
    this.id = arrayOfObject[0];
    this.product = arrayOfObject[1];
    this.product2 = arrayOfObject[2];
    this.name = arrayOfObject[5];
    this.m_jReference.setText(Formats.STRING.formatValue(arrayOfObject[3]));
    this.m_jBarcode.setText(Formats.STRING.formatValue(arrayOfObject[4]));
    this.m_jProduct.setText(Formats.STRING.formatValue(arrayOfObject[3]) + " - " + Formats.STRING.formatValue(arrayOfObject[5]));
    this.m_jReference.setEnabled(true);
    this.m_jBarcode.setEnabled(true);
    this.m_jProduct.setEnabled(true);
    this.m_jEnter1.setEnabled(true);
    this.m_jEnter2.setEnabled(true);
    this.m_jSearch.setEnabled(true);
  }

  public void writeValueDelete(Object paramObject)
  {
    Object[] arrayOfObject = (Object[])(Object[])paramObject;
    this.id = arrayOfObject[0];
    this.product = arrayOfObject[1];
    this.product2 = arrayOfObject[2];
    this.name = arrayOfObject[5];
    this.m_jReference.setText(Formats.STRING.formatValue(arrayOfObject[3]));
    this.m_jBarcode.setText(Formats.STRING.formatValue(arrayOfObject[4]));
    this.m_jProduct.setText(Formats.STRING.formatValue(arrayOfObject[3]) + " - " + Formats.STRING.formatValue(arrayOfObject[5]));
    this.m_jReference.setEnabled(false);
    this.m_jBarcode.setEnabled(false);
    this.m_jProduct.setEnabled(false);
    this.m_jEnter1.setEnabled(false);
    this.m_jEnter2.setEnabled(false);
    this.m_jSearch.setEnabled(false);
  }

  public Object createValue()
    throws BasicException
  {
    return new Object[] { this.id, this.product, this.product2, this.m_jReference.getText(), this.m_jBarcode.getText(), this.name };
  }

  public Component getComponent()
  {
    return this;
  }

  private void assignProduct(ProductInfoExt paramProductInfoExt)
  {
    if (this.m_jSearch.isEnabled())
      if (paramProductInfoExt == null)
      {
        this.product2 = null;
        this.m_jReference.setText(null);
        this.m_jBarcode.setText(null);
        this.m_jProduct.setText(null);
        this.name = null;
      }
      else
      {
        this.product2 = paramProductInfoExt.getID();
        this.m_jReference.setText(paramProductInfoExt.getReference());
        this.m_jBarcode.setText(paramProductInfoExt.getCode());
        this.m_jProduct.setText(paramProductInfoExt.getReference() + " - " + paramProductInfoExt.getName());
        this.name = paramProductInfoExt.getName();
      }
  }

  private void assignProductByCode()
  {
    try
    {
      ProductInfoExt localProductInfoExt = this.m_dlSales.getProductInfoByCode(this.m_jBarcode.getText());
      assignProduct(localProductInfoExt);
      if (localProductInfoExt == null)
        Toolkit.getDefaultToolkit().beep();
    }
    catch (BasicException localBasicException)
    {
      assignProduct(null);
      MessageInf localMessageInf = new MessageInf(localBasicException);
      localMessageInf.show(this);
    }
  }

  private void assignProductByReference()
  {
    try
    {
      ProductInfoExt localProductInfoExt = this.m_dlSales.getProductInfoByReference(this.m_jReference.getText());
      assignProduct(localProductInfoExt);
      if (localProductInfoExt == null)
        Toolkit.getDefaultToolkit().beep();
    }
    catch (BasicException localBasicException)
    {
      assignProduct(null);
      MessageInf localMessageInf = new MessageInf(localBasicException);
      localMessageInf.show(this);
    }
  }

  private void initComponents()
  {
    this.jLabel3 = new JLabel();
    this.m_jReference = new JTextField();
    this.m_jEnter1 = new JButton();
    this.m_jEnter2 = new JButton();
    this.m_jSearch = new JButton();
    this.m_jProduct = new JTextField();
    this.m_jBarcode = new JTextField();
    this.jLabel4 = new JLabel();
    this.jLabel3.setText(AppLocal.getIntString("label.prodref"));
    this.m_jReference.addActionListener(new ActionListener()
    {
      public void actionPerformed(ActionEvent paramActionEvent)
      {
        AuxiliarEditor.this.m_jReferenceActionPerformed(paramActionEvent);
      }
    });
    this.m_jEnter1.setIcon(new ImageIcon(getClass().getResource("/com/openbravo/images/apply.png")));
    this.m_jEnter1.addActionListener(new ActionListener()
    {
      public void actionPerformed(ActionEvent paramActionEvent)
      {
        AuxiliarEditor.this.m_jEnter1ActionPerformed(paramActionEvent);
      }
    });
    this.m_jEnter2.setIcon(new ImageIcon(getClass().getResource("/com/openbravo/images/apply.png")));
    this.m_jEnter2.addActionListener(new ActionListener()
    {
      public void actionPerformed(ActionEvent paramActionEvent)
      {
        AuxiliarEditor.this.m_jEnter2ActionPerformed(paramActionEvent);
      }
    });
    this.m_jSearch.setIcon(new ImageIcon(getClass().getResource("/com/openbravo/images/search.png")));
    this.m_jSearch.addActionListener(new ActionListener()
    {
      public void actionPerformed(ActionEvent paramActionEvent)
      {
        AuxiliarEditor.this.m_jSearchActionPerformed(paramActionEvent);
      }
    });
    this.m_jProduct.setEditable(false);
    this.m_jProduct.addActionListener(new ActionListener()
    {
      public void actionPerformed(ActionEvent paramActionEvent)
      {
        AuxiliarEditor.this.m_jProductActionPerformed(paramActionEvent);
      }
    });
    this.m_jBarcode.addActionListener(new ActionListener()
    {
      public void actionPerformed(ActionEvent paramActionEvent)
      {
        AuxiliarEditor.this.m_jBarcodeActionPerformed(paramActionEvent);
      }
    });
    this.jLabel4.setText(AppLocal.getIntString("label.prodbarcode"));
    GroupLayout localGroupLayout = new GroupLayout(this);
    setLayout(localGroupLayout);
    localGroupLayout.setHorizontalGroup(localGroupLayout.createParallelGroup(GroupLayout.Alignment.LEADING).addGroup(localGroupLayout.createSequentialGroup().addContainerGap().addGroup(localGroupLayout.createParallelGroup(GroupLayout.Alignment.LEADING).addGroup(localGroupLayout.createSequentialGroup().addGroup(localGroupLayout.createParallelGroup(GroupLayout.Alignment.LEADING, false).addGroup(localGroupLayout.createSequentialGroup().addComponent(this.jLabel3, -2, 125, -2).addPreferredGap(LayoutStyle.ComponentPlacement.RELATED).addComponent(this.m_jReference, -2, 241, -2)).addGroup(GroupLayout.Alignment.TRAILING, localGroupLayout.createSequentialGroup().addComponent(this.jLabel4, -2, 125, -2).addPreferredGap(LayoutStyle.ComponentPlacement.RELATED, -1, 32767).addComponent(this.m_jBarcode, -2, 241, -2))).addPreferredGap(LayoutStyle.ComponentPlacement.RELATED).addGroup(localGroupLayout.createParallelGroup(GroupLayout.Alignment.LEADING).addComponent(this.m_jEnter2).addComponent(this.m_jEnter1))).addGroup(localGroupLayout.createSequentialGroup().addComponent(this.m_jProduct, -2, 378, -2).addPreferredGap(LayoutStyle.ComponentPlacement.RELATED).addComponent(this.m_jSearch))).addContainerGap(-1, 32767)));
    localGroupLayout.setVerticalGroup(localGroupLayout.createParallelGroup(GroupLayout.Alignment.LEADING).addGroup(localGroupLayout.createSequentialGroup().addContainerGap().addGroup(localGroupLayout.createParallelGroup(GroupLayout.Alignment.LEADING).addComponent(this.m_jEnter1).addGroup(localGroupLayout.createParallelGroup(GroupLayout.Alignment.BASELINE).addComponent(this.jLabel3).addComponent(this.m_jReference, -2, -1, -2))).addPreferredGap(LayoutStyle.ComponentPlacement.RELATED).addGroup(localGroupLayout.createParallelGroup(GroupLayout.Alignment.LEADING).addGroup(localGroupLayout.createParallelGroup(GroupLayout.Alignment.BASELINE).addComponent(this.m_jBarcode, -2, -1, -2).addComponent(this.jLabel4)).addComponent(this.m_jEnter2)).addPreferredGap(LayoutStyle.ComponentPlacement.RELATED).addGroup(localGroupLayout.createParallelGroup(GroupLayout.Alignment.LEADING).addComponent(this.m_jSearch).addComponent(this.m_jProduct, -2, -1, -2)).addContainerGap(-1, 32767)));
  }

  private void m_jSearchActionPerformed(ActionEvent paramActionEvent)
  {
    assignProduct(JProductFinder.showMessage(this, this.m_dlSales, 2));
  }

  private void m_jReferenceActionPerformed(ActionEvent paramActionEvent)
  {
    assignProductByReference();
  }

  private void m_jEnter2ActionPerformed(ActionEvent paramActionEvent)
  {
    assignProductByCode();
  }

  private void m_jEnter1ActionPerformed(ActionEvent paramActionEvent)
  {
    assignProductByReference();
  }

  private void m_jBarcodeActionPerformed(ActionEvent paramActionEvent)
  {
    assignProductByCode();
  }

  private void m_jProductActionPerformed(ActionEvent paramActionEvent)
  {
  }
}

/* Location:           D:\PowerFulPOS-2.30.2\openbravopos.jar
 * Qualified Name:     com.openbravo.pos.inventory.AuxiliarEditor
 * JD-Core Version:    0.6.0
 */